<?php 
class ModelPhoneComment extends Model {
	public function getComments($data = array()) {
		$sql = "SELECT phone_comment_id, nickname, comment, pc.rate, pc.created, pc.call_type_id FROM `phone_comments` pc JOIN `phone_call_types` pct ON pc.`call_type_id` = pct.`call_type_id`";

		$sort_data = array(
			'pc.phone_comment_id',
		);	
		
		if (isset($data['phone_id'])) {
			$sql .= " Where pc.phone_id = '" . (int)$data['phone_id'] ."'";
		}
		
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY pc.phone_comment_id";	
		}	

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}				

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}	

		$query = $this->db->query($sql);
		
		return $query->rows;
	}
}
?>